{% extends 'index.html' %}
{% block ace-content %}
<!-- bootstrap-notify -->   
<script type="text/javascript" src="/static/dist/js/bootstrap-notify.js"></script>
<script type="text/javascript" src="/static/js/reconnecting-websocket.js"></script>
{% endblock %}
{% block page-content %}
<div id="page-wrapper">
    <div class="row">
         <div class="col-lg-12">
              <h1 class="page-header"><i class="fa  fa-user  "></i> 用户中心</h1>
         </div>
                <!-- /.col-lg-12 -->
    </div>
	<div class="row">
                <div class="col-lg-12">
                    <div class="panel panel-default">
                        <div class="panel-heading">
                           	<i class="fa   fa-gear"></i> 用户管理
                        </div>
                        <div class="panel-body">

				                            
				           <ul class="nav nav-tabs">
				              <li class="active"><a href="#home" data-toggle="tab"><i class="fa  fa-tasks"></i> 我的工单</a>
				              </li>
				              {% if serverList %}
				              <li><a href="#server" data-toggle="tab"><i class="fa fa-linux"></i> 我的主机</a>
				              </li>			
				              {% endif %}	              
				              <li><a href="#profile" data-toggle="tab"><i class="fa   fa-lock"></i> 修改密码</a>
				              </li>
				           </ul>
				
				                            
				           <div class="tab-content">
				              <div class="tab-pane fade in active" id="home">
				                   <div class="row">
				                   		<div class="col-lg-12">
				                   			<br>
				                            <table width="100%" class="table table-striped table-bordered table-hover" id="orderTableList">
				                                <thead>
				                                    <tr>
				                                    	<th class="text-center">ID</th>
				                                    	<th class="text-center">工单类型</th>
				                                    	<th class="text-center">工单申请人</th>
				                                    	<th class="text-center">工单主题</th>
				                                        <th class="text-center">执行人</th>
				                                        <th class="text-center">发布时间</th>
				                                        <th class="text-center">工单状态</th>
				                                        <th class="text-center">操作</th>
				                                    </tr>
				                                </thead>
				                                <tbody>
				                                	{% for ds in orderList %}
                                		<tr class="odd gradeX">
                                			<td class="text-center">{{ds.id}} </td>
											<td class="text-center">
												{% if ds.order_type == 1 %}
													<span class="label label-success">代码更新</span>
												{% elif ds.order_type == 2 %}
													<span class="label label-warning">文件分发</span>
												{% elif ds.order_type == 0 %}
													<span class="label label-info">SQL更新</span>
												{% elif ds.order_type == 3 %}
													<span class="label label-danger">文件下载</span>												
												{% endif %}
                                			</td>  
                                			<td class="text-center">
												{{ds.order_user}}
                                			</td>                              			
                                			<td class="text-center">
                                				{{ds.order_subject}}
                                			</td>
                                			<th class="text-center">{{ds.order_executor}}</th>
                                			<th class="text-center">{{ds.create_time|date:"Y/m/d H:i  "}}</th>
                                			<td class="text-center">
                                				{% if ds.order_status == 0 %}
                                					<span class="label label-success">已通过</span>                                			
                                				{% elif ds.order_status == 1 %}
                                					<span class="label label-danger">已拒绝</span>
                                				{% elif ds.order_status == 2 %}
                                					<span class="label label-info">审核中</span>
                                				{% elif ds.order_status == 3 %}
                                					<span class="label label-success">已部署</span>  
                                				{% elif ds.order_status == 4 %}
                                					<span class="label label-info">待授权</span>     
                                				{% elif ds.order_status == 5 %}
                                					<span class="label label-success">已执行</span>                                 					                            					
                                				{% elif ds.order_status == 6 %}
                                					<span class="label label-default">已回滚</span>     
                                				{% elif ds.order_status == 7 %}
                                					<span class="label label-danger">已撤回</span>   
                                				{% elif ds.order_status == 8 %}
                                					<span class="label label-warning">已授权</span>                                  					                             					                         					
                                				{% else %}
                                					<span class="label label-danger">已失败</span>
                                				{% endif %}
                                			</td>
                                			<td class="text-center">  
                                				{% if user.is_superuser %}
                           							{% if ds.order_type == 1 and ds.order_status == 3 %}                               						
                                						<a href="/deploy_order/rollback/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="回滚代码"><i class="fa fa-play-circle-o"></i></button></a>	
                                					{% elif ds.order_type == 1  %}	
                                						<a href="/deploy_order/status/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="部署代码"><i class="fa fa-play-circle-o"></i></button></a>	
                                					{% elif ds.order_type == 0 %}
                                						<a href="/db/sql/order/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="更新SQL"><i class="fa fa-play-circle-o"></i></button></a>	
                                					{% elif ds.order_type == 2 %}
                                						<a href="/file/upload/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="更新SQL"><i class="fa fa-play-circle-o"></i></button></a>
                                					{% elif ds.order_type == 3 %}
                                						<a href="/file/download/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="文件下载"><i class="fa fa-play-circle-o"></i></button></a>                                						                            						                             							                                						
                                					{% endif %}		
													{% if ds.order_status == 4 or ds.order_status == 2 %}
														<button  type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'auth',8)"><abbr title="授权"><i class="fa fa-check"></i></button>
														<button  type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'disable',7)"><abbr title="取消"><i class="fa fa-times"></i></button>
													{% elif ds.order_status == 8 %}
														<button  type="button" class="btn btn-default"><abbr title="授权"><i class="fa fa-check"></i></button>	
														<button  type="button" class="btn btn-default"><abbr title="取消"><i class="fa fa-times "></i></button>
													{% elif ds.order_status == 5 %}
														<button  type="button" class="btn btn-default"><abbr title="授权"><i class="fa fa-check"></i></button>
														<button  type="button" class="btn btn-default"><abbr title="取消"><i class="fa fa-times "></i></button>											
													{% else %}
														<button  type="button" class="btn btn-default"><abbr title="授权"><i class="fa fa-check"></i></button>
														<button  type="button" class="btn btn-default disabled" ><abbr title="取消"><i class="fa fa-times "></i></button>		
													{% endif %}														
													<button  type="button" class="btn btn-default" onclick="deleteOrder(this,{{ds.id}})"><abbr title="删除"><i class="glyphicon glyphicon-trash"></i></button>
												{% else %}
													{% if ds.perm %}
	                           							{% if ds.order_type == 1 and ds.order_status == 3 %}                               						
	                                						<a href="/deploy_order/rollback/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="回滚代码"><i class="fa fa-play-circle-o"></i></button></a>	
	                                					{% elif ds.order_type == 1  %}	
	                                						<a href="/deploy_order/status/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="部署代码"><i class="fa fa-play-circle-o"></i></button></a>	
	                                					{% elif ds.order_type == 0 %}
	                                						<a href="/db/sql/order/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="更新SQL"><i class="fa fa-play-circle-o"></i></button></a>	
	                                					{% elif ds.order_type == 2 %}
	                                						<a href="/file/upload/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="更新SQL"><i class="fa fa-play-circle-o"></i></button></a>	                                						
	                                					{% elif ds.order_type == 3 %}
	                                						<a href="/file/download/run/{{ds.id}}/" target="_blank"><button  type="button" class="btn btn-default"><abbr title="文件下载"><i class="fa fa-play-circle-o"></i></button></a>                                						                            						
	                                					{% endif %}		
														{% if ds.order_status == 4 or ds.order_status == 2 %}
															<button type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'auth',8)"><abbr title="授权"><i class="fa fa-check"  ></i></button>
															<button type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'disable',7)"><abbr title="取消"><i class="fa fa-times"  ></i></button>
														{% elif ds.order_status == 8 %}
															<button type="button" class="btn btn-default"><abbr title="授权"><i class="fa fa-check"></i></button>	
															<button type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'disable',7)"><abbr title="取消"><i class="fa fa-times "  ></i></button>		
														{% elif ds.order_status == 5 %}
															<button type="button" class="btn btn-default"><abbr title="授权"><i class="fa fa-check"></i></button>	
															<button type="button" class="btn btn-default"><abbr title="取消"><i class="fa fa-times"></i></button>																			
														{% else %}
															<button type="button" class="btn btn-default"><abbr title="授权"><i class="fa fa-check"></i></button>
															<button type="button" class="btn btn-default" onclick="updateOrderStatus(this,{{ds.id}},'disable',7)"><abbr title="取消"><i class="fa fa-times">  </i></button>	
														{% endif %}											
													{% else %}
														<button type="button" class="btn btn-default"><abbr title="执行SQL"><i class="fa fa-play-circle-o"></i></button>
														<button type="button" class="btn btn-default"><abbr title="授权"><i class="fa fa-check" ></i></button>
														<button type="button" class="btn btn-default"><abbr title="取消"><i class="fa fa-times "></i></button>															
													{% endif %}
                                				{% endif %}
								           				
                                				
                                			</td>
                                		</tr>
				                                	{% endfor %}
				                                </tbody>
				                            </table>			                   		
				                   		</div>

				                   </div>
				              </div>
				              {% if serverList %}
				              <div class="tab-pane fade" id="server">
				                   <div class="row">
				                   		<div class="col-sm-12">
				                   			<br>	
				                            <table width="100%" class="table table-striped table-bordered table-hover" id="assetsListTable">
				                                <thead>
													  <tr>
													       <th class="text-center">资产类型</th>
													       <th class="text-center">管理IP</th>
													       <th class="text-center">资产编号</th>
													       <th class="text-center">设备型号</th>
													       <th class="text-center">放置区域</th>
													       <th class="text-center">业务类型</th>
													       <th class="text-center">使用组</th>
													       <th class="text-center">购买时间</th>									       
													       <th class="text-center">设备状态</th>
													       <th class="text-center">操作</th>
													    </tr>
				                                </thead>
				                                <tbody>
				                                	{% for a in  serverList %}
					                                    <tr class="odd gradeX">
													         <td class="text-center">	
													         		{% if a.assets_type == "server" %}
													         			服务器
																	{% elif a.assets_type == "vmser" %}
									         							虚拟机												         			
													         		{% elif a.assets_type == "switch" %}
													         			交换机
													         		{% elif a.assets_type == "route" %}
													         			路由器
													         		{% elif a.assets_type == "printer" %}
													         			打印机
													         		{% elif a.assets_type == "scanner" %}
													         			扫描仪
													         		{% elif a.assets_type == "firewall" %}
													         			防火墙
													         		{% elif a.assets_type == "storage" %}
													         			存储设备
													         		{% elif a.assets_type == "wifi" %}
													         			无线设备							         													         												         												         											         												         			
													         		{% endif %}
													         </td>
													         <td class="text-center">
													         	{% if a.management_ip %}
													         		{{ a.management_ip }}
													         	{% else %}
													         		{{ a.server_assets.ip }}
													         	{% endif %}													         
													         </td>
													         <td class="text-center">{{ a.name }}</td>
													         <td class="text-center">{{ a.model|default:"无" }}</td>
													         <td class="text-center">
													         	{% for z in baseAssets.zone %}
													         		{% if z.id == a.put_zone %}
													         			{{ z.zone_name }}
													         		{% endif %}
													         	{% endfor %}
													         </td>
													         <td class="text-center">
													         	{% for s in baseAssets.service %}
													         		{% if s.id == a.business %}
													         			{{ s.service_name }}	
													         		{% endif %}
													         	{% endfor %}													         
													         </td>
													         <td class="text-center">
													         	{% for g in baseAssets.group %}
													         		{% if g.id == a.put_zone %}
													         			{{ g.name }}
													         		{% endif %}
													         	{% endfor %}													         
													         </td>
													         <td class="text-center">
													         	{% if a.buy_time %}
													         		{{ a.buy_time|date:"Y/m/d" }}
													         	{% else %}
													         		{{ a.create_date|date:"Y/m/d" }}
													         	{% endif %}									         		
													         </td>
													         <td class="text-center">
													         		{% if a.status == 0 %}
													         			<button  type="button" class="btn btn-outline btn-success">已上线</button>
													         		{% elif a.status == 1 %}
													         			<button  type="button" class="btn btn-outline btn-primary">已下线</button>
													         		{% elif a.status == 2 %}
													         			<button  type="button" class="btn btn-outline btn-warning">维修中</button>
													         		{% elif a.status == 3 %}
													         			<button  type="button" class="btn btn-outline btn-info">已入库</button>
													         		{% elif a.status == 4 %}
													         			<button  type="button" class="btn btn-outline btn-default">未使用</button>
													         		{% endif %}
													         </td>	
													         <td class="text-center">
													         	<a href="/assets_view/{{a.id}}" style="text-decoration:none;"><button  type="button" class="btn btn-default"><abbr title="查看详细信息"><i class="glyphicon glyphicon-info-sign"></i></abbr></button></a>

													         	{% if a.server_assets.id %}
													         		<a href="/webssh/{{a.server_assets.id}}/" style="text-decoration:none;" target="_blank"><button  type="button" class="btn btn-default"><abbr title="WebSSH"><i class="fa fa-desktop"></i></abbr></button></a>
													         	{% else %}
													         		<button  type="button" class="btn btn-default" disabled><abbr title="WebSSH"><i class="fa fa-desktop"></i></abbr></button>
													         	{% endif %}
													         </td>
					                                    </tr>
					                                 {% endfor %}
				                                </tbody>
				                            </table>				                   					                   			
										</div>
				                   </div>
				              </div>	
				              {% endif %}			              
				              <div class="tab-pane fade" id="profile">
				                   <div class="row">
				                   		<div class="col-sm-2"></div>
				                   		<div class="col-sm-8">
				                   			<br>
											<form role="form" method="post" id="modfPasswd" class="main form-horizontal" >{% csrf_token %}			
												<div class="form-group">
													<label class="col-sm-2 control-label"><i class="fa fa-lock"></i></label>
													<div class="col-sm-4">
														<input type="password" class="form-control" placeholder="Password" name="password" />
													</div>
												</div>
												<div class="form-group">
													<label class="col-sm-2 control-label"><i class="fa fa-retweet"></i></label>
													<div class="col-sm-4">
														<input type="password" class="form-control" placeholder="Repeat password" name="c_password"/>
													</div>
												</div>																																											
												
												<div class="hr hr32 hr-dotted"></div>                                        
												<div class="form-group">
													<div class="col-md-offset-3 col-md-9">
														<button class="btn btn-default" type="buttun" onclick="modfUserPasswd(this)">
															<i class="fa fa-check"></i>
																			提交
														</button>						
																		&nbsp; &nbsp; &nbsp;
														<button class="btn btn-default" type="reset">
															<i class="fa fa-undo"></i>
																			撤销
														</button>
													</div>
												</div>
											</form>				                   			
										</div>

				                   </div>
				              </div>
				           </div>


                            <!-- /.row (nested) -->
                        </div>
                        <!-- /.panel-body -->
                    </div>
                    <!-- /.panel -->
                </div>
                <!-- /.col-lg-12 -->
            </div>    
</div>


<script type="text/javascript">

		$(document).ready(function() {
		    $('#assetsListTable').DataTable({
		        responsive: true
		    });
		});


	    $(document).ready(function() {
	        $('#orderTableList').DataTable({
	        	responsive: true,
 		        "order": [[ 0, 'desc' ], [ 6, 'desc' ]]
	        });
	    } );
	
		function modfUserPasswd(obj) {
			var btnObj = $(obj);
			btnObj.attr('disabled',true);
			var post_data = {}; 
			var form = document.getElementById('modfPasswd');
			var required = ["c_password","password"];
			for (var i = 1; i < form.length; ++i) {
				var name = form[i].name;
				var value = form[i].value;
				idx = $.inArray(name, required);
				if (idx >= 0  && value.length == 0){
					window.wxc.xcConfirm("请注意必填项不能为空~", window.wxc.xcConfirm.typeEnum.error);
					return false;
				}
				else{
					post_data[name] = value;
				};
				
			};
			$.ajax({
				dataType: "JSON",
				url:'/user/center/', //请求地址
				type:"POST",  //提交类似
				data:post_data, //提交参数
				success:function(response){
					btnObj.removeAttr('disabled');
					window.wxc.xcConfirm(response["msg"], window.wxc.xcConfirm.typeEnum.success);
// 					location.reload();
				},
		    	error:function(response){
		    		btnObj.removeAttr('disabled');
		    		window.wxc.xcConfirm("密码修改失败", window.wxc.xcConfirm.typeEnum.error);
// 		    		location.reload();
		    	}
			})	
		}
		
		function updateOrderStatus(obj,id,model,status){
			if (model == 'auth' || model == 'finish' ){
				if (model == 'auth'){
					var txt=  "是否确认授权？";
					var title = "部署工单授权";
				}
				else if (model == 'finish'){
					var txt=  "是否确认完成？";
					var title = "部署工单完成";				
				};
				var option = {
					title: title,
					btn: parseInt("0011",2),
					onOk: function(){
						$.ajax({
							  type: 'PUT',
							  url: '/api/orders/'+id+'/',
							  data: {
								  "id":id,
								  "model":model,
								  "order_status":status
							  },
						      success:function(response){			            
					             window.wxc.xcConfirm("授权成功", window.wxc.xcConfirm.typeEnum.success);
					             location.reload();

					                
							},
				            error:function(response){
				            	window.wxc.xcConfirm("请求数据错误！", window.wxc.xcConfirm.typeEnum.error);
				            },
							});
					},
					onCancel:function(){	
					},
					onClose:function(){
					}
				}
				window.wxc.xcConfirm(txt, "custom", option);				
			} 
			else if (model == 'disable'){
				window.wxc.xcConfirm("撤销原因：", window.wxc.xcConfirm.typeEnum.input,{
					onOk:function(result){
						if (result.length == 0){
							/* 如果没有输入字符串则直接退出 */
							return;
						};
						$.ajax({
							  type: 'PUT',
							  url: '/api/orders/'+id+'/',
							  data: {
								  "model":model,
								  'order_cancel':result,
								  'id':id,
								  "order_status":status
							  },
						      success:function(response){	
					                window.wxc.xcConfirm("取消失败", window.wxc.xcConfirm.typeEnum.success);
					                btnObj.removeAttr('disabled');
							},
				            error:function(response){
				            	btnObj.removeAttr('disabled');
				            	window.wxc.xcConfirm("服务器响应错误，撤销失败", window.wxc.xcConfirm.typeEnum.error);
				            },							  
							});
						
					}
				})			
			}
	   }	  
</script>
	{% if config.email == 1 %}
	<audio id="notifySound"><source src="/static/misc/hx.mp3" type="audio/mp3"> </audio>
	<script type="application/javascript">
	    var ws_scheme = window.location.protocol == "https:" ? "wss" : "ws";
	    var ws = new ReconnectingWebSocket(ws_scheme + '://' + window.location.host + "/ws/notice/{{user}}/",null, {debug: true, reconnectInterval: 3000} ); 
	    ws.onmessage = function (message) {
	        var data = JSON.parse(message.data);
	        console.log(data)
	        $('#notifySound')[0].play();
			$.notify({
				title: "<strong>"+ data['title'] +"</strong>",
				message: data['messages']
			},
			{
				type: data['type'],
				url_target: '_blank',
				delay: 3600000
			});	
	    }
	    ws.onclose = function(){
	        console.log('closed....');
	    };
	</script>
	{% endif %}
{% endblock %}